package com.dykj.dati.generator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.StringUtils;

import java.util.Scanner;

/**
* GeneratorMybatisCodeConfig Class
* @author: yc
* @Created: 2021/2/5 14:30
*/

public class GeneratorNew {
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        DataSourceConfig dsc=new DataSourceConfig
                .Builder("jdbc:mysql://10.101.1.35:3306/dt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&allowMultiQueries=true&useAffectedRows=true"
                ,"root"
                ,"123456")
                .build();
        AutoGenerator mpg=new AutoGenerator(dsc);
        String ModuleName=scanner("所属模块名");
        String projectPath = System.getProperty("user.dir");
        GlobalConfig gc = new GlobalConfig
                .Builder()
                .outputDir(projectPath + "/src/main/java")
                .author("YangChao")
                .openDir(false)
                .build();
        PackageConfig pc = new PackageConfig
                .Builder()
                .parent("com.dykj.dati")
                .entity("model."+ModuleName)
                .controller("controller."+ModuleName)
                .mapper("mapper."+ModuleName)
                .service("service."+ModuleName)
                .serviceImpl("service."+ModuleName+".impl")
               /* .moduleName(ModuleName)*/
                .build();
        TemplateConfig templateConfig=new TemplateConfig
                .Builder()
                .controller(null)
                .mapperXml(null)
                .build();
        StrategyConfig strategy = new StrategyConfig
                .Builder()
                .addInclude(scanner("表名，多个英文逗号分割").split(","))
                .addTablePrefix(pc.getModuleName() + "_")
                .entityBuilder()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .enableLombok()
                .superClass("com.baomidou.mybatisplus.extension.activerecord.Model")
                .controllerBuilder()
                .enableHyphenStyle()
                .build();
        mpg.global(gc);
        mpg.packageInfo(pc);
        mpg.template(templateConfig);
        mpg.strategy(strategy);
        mpg.execute(new FreemarkerTemplateEngine());
    }
}
